package com.hippo.javacore.multithreading;

/**
 * @ClassName ThreadDemo5
 * @Description TODO 常见线程方法--线程优先级--java是抢占式调度
 * @Author tangxl
 * @create 2023-03-13 08:56
 **/
public class ThreadDemo5 {

    public static void main(String[] args) {
        // 实现重写call()方法
        Runnable runnable = ()->{
            for (int i = 0; i < 100 ;i++) {
                System.out.println(Thread.currentThread().getName() + "输出：" + i);
            }
        };

        // 创建线程
        Thread t1 = new Thread(runnable,"线程1");
        Thread t2 = new Thread(runnable,"线程2");

        // 获取线程优先级
        int priority1 = t1.getPriority();
        int priority2 = t1.getPriority();
        System.out.println(t1.getName() + "优先级：" + priority1);
        System.out.println(t2.getName() + "优先级：" + priority2);

        // 修改线程优先级
        t1.setPriority(10);
        t2.setPriority(1);
        System.out.println(t1.getName() + "修改后优先级：" + t1.getPriority());
        System.out.println(t2.getName() + "修改后优先级：" + t2.getPriority());

        // 执行线程任务
        t1.start();
        t2.start();

        // 总结：线程优先级范围1~10，默认5，线程优先级越小,线程越优先执行
    }
}
